package communication;

import java.io.Serializable;

public final class MessageHelper {
	
	public static final String RESULT = "result";
	
	public static final String SUCCESS = "success";
	
	public static final String FAILED = "failed";
	
	public static final String EMPTY = "";
	
	private MessageHelper() {
	}
	
	public static void markAsFailed(ParameterizedMessage msg, Throwable error) {
		msg.setParameter(RESULT, FAILED);
		msg.setAttachment(error);
	}
	
	public static void markAsSuccess(ParameterizedMessage msg, Serializable resultAttachment) {
		msg.setParameter(RESULT, SUCCESS);
		
		msg.setAttachment(resultAttachment);
	}
	
	public static ParameterizedMessage buildSuccessMessage() {
		ParameterizedMessage msg = new ParameterizedMessageImpl(SUCCESS);
		msg.setParameter(RESULT, SUCCESS);
		
		return msg;
	}
	
	public static boolean isSucceed(ParameterizedMessage msg) {
		return msg.getParameter(RESULT).equals(SUCCESS);
	}
	
	public static String getNonNullParameter(ParameterizedMessage msg, String parameterName) {
		String ret = msg.getParameter(parameterName);
		if (ret != null) {
			return ret;
		}
		
		return EMPTY;
	}

}
